<?php
namespace submodule\modules\user\components;

use Yii;
use yii\db\Schema;
use yii\db\Migration;

class Uninstaller extends Migration
{
	public $controller;
	
	public function __construct($controller){
		$this->controller = $controller;
		
	}
    //安装
    public function install($option, $field_tablename)
    {
		$modelid = $option['modelid'];
		
		$modelids = [$modelid];
		//建表
        Yii::$app->db->createCommand()->createTable('{{%'.$option['tablename'].'}}', [
			'userid' => "pk",
			'uuid' => "char(36) COMMENT 'UUID'",
			'username' => "varchar(50) COMMENT '用户名'",
			'password' => "char(32) default '' COMMENT '密码'",
			'encrypt' => "char(6) default '' COMMENT '密码加密'",
			'mobileno' => "int(11) default NULL COMMENT '手机号码'",
			'email' => "varchar(50) default '' COMMENT '邮箱'",
			'nikename' => "varchar(50) COMMENT '昵称'",
			'avatar' => "varchar(150) default '' COMMENT '头像'",
			'lastlogin' => "int(10) default NULL COMMENT '最后登录时间'",
			'status' => "tinyint(1) default '0' COMMENT '状态'",
        ],'ENGINE InnoDB DEFAULT CHARSET utf8')->execute();
		
		$sql = [];
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, 'userid', 'userid', 'primarykey', 'int', '10', 0, 0, 1, 1, '', '', NULL, 0)";
		
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '用户标识码', 'uuid', 'text', 'char', '36', 0, 0, 1, 1, '', '', NULL, 0)";
		
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '用户名', 'username', 'text', 'varchar', '50', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '用户密码', 'password', 'text', 'char', '36', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '密码加密', 'encrypt', 'text', 'char', '6', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '手机号', 'mobileno', 'text', 'int', '11', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '邮箱', 'email', 'text', 'varchar', '50', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '昵称', 'nikename', 'text', 'varchar', '50', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '头像', 'avatar', 'image', 'varchar', '150', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '最后登录', 'lastlogin', 'text', 'int', '10', 0, 0, 1, 1, '', '', NULL, 1)";
		
		$sql[] = "INSERT INTO $tablename (`modelid`, `name`, `field`, `formtype`, `fieldtype`, `fieldlength`, `listorder`, `disabled`, `primarytable`, `required`, `setting`, `param`, `api`, `allowinput`) VALUES ($modelid, '状态', 'status', 'text', 'tinyint', '1', 0, 0, 1, 1, '', '', NULL, 1)";

		foreach($sql as $v){
			Yii::$app->db->createCommand($v)->execute();
		}
		return $modelids;
		
    }
    
	
	//卸载
    public function uninstall()
    {
        $this->dropTable('{{store_product}}');
    }
}
